import java.util.PriorityQueue;

public class T {

    static int id=10;
    public static void main(String[] args) {



        Runnable run=new Runnable() {
            @Override
            public void run() {
                id=100;
                System.out.println(id);
            }
        };

        MyTimer myTimer=new MyTimer();

        myTimer.schedule(()->{
            System.out.println("3000");
        },3000);

        myTimer.schedule(()->{
            System.out.println("2000");
        },2000);

        myTimer.schedule(()->{
            System.out.println("1000");
        },1000);

    }

    public void func(){
        System.out.println(id);
    }

}



class Task implements Comparable<Task>{
    @Override
    public int compareTo(Task o) {
        return (int) (this.time-o.time);
    }

    long time;
    Runnable runnable;


    public Task(Runnable runnable, long delay){

        this.runnable=runnable;
        this.time=delay+System.currentTimeMillis();


    }


}



class MyTimer{

    PriorityQueue<Task> priorityQueue=new PriorityQueue<>();



    public void schedule(Runnable runnable, long delay){

        synchronized (this){

            priorityQueue.offer(new Task(runnable,delay));
            this.notify();
        }
    }







    public MyTimer(){

        Thread t=new Thread(()->{
            while(true){

                synchronized (this) {

                    try {

                        while (priorityQueue.isEmpty()) {
                            this.wait();
                        }


                        Task task = priorityQueue.peek();
                        long curerntTime = System.currentTimeMillis();

                        if (curerntTime < task.time) {

                            this.wait(  task.time-curerntTime);

                        } else {
                            priorityQueue.poll();
                            task.runnable.run();
                        }
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }
                }

            }


        });

        t.start();

    }



}
